Q-3: Intents in Android OR Toolbar in Android (5 Marks)
Questions​
a) What are Intents in Android, and how are they used? (5 marks)
OR
b) What is the Toolbar in Android, and how is it used? (5 marks)
Answers​
a) Intents in Android and their Usage​
Definition​
Intent is a messaging object that facilitates communication between different components of an Android application or between different applications.
Purpose​
Intents serve as a bridge for:
- Component Communication: Connect activities, services, broadcast receivers
- Data Transfer: Pass data between components
- Action Requests: Request specific actions from other components
- Application Integration: Enable inter-app communication
Types of Intents​
1. Explicit Intents
- Definition: Specify the exact component to be invoked
- Usage: Navigation within the same application
- Target: Known component (activity, service)
Example:
Intent explicitIntent = new Intent(MainActivity.this, SecondActivity.class);
explicitIntent.putExtra("message", "Hello from MainActivity");
startActivity(explicitIntent);
2. Implicit Intents
- Definition: Don't specify exact component, but declare action to be performed
- Usage: Request actions from other apps or system
- Target: Any component that can handle the action
Example:
// Open web browser
Intent webIntent = new Intent(Intent.ACTION_VIEW);
webIntent.setData(Uri.parse("https://www.google.com"));
startActivity(webIntent);
// Send email
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"test@example.com"});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
startActivity(emailIntent);
Common Intent Actions​
- ACTION_VIEW: Display data to user
- ACTION_SEND: Share data with other apps
- ACTION_CALL: Make phone call
- ACTION_DIAL: Open dialer with number
- ACTION_EDIT: Edit data
- ACTION_PICK: Select item from data
Data Transfer with Intents​
// Sending data
Intent intent = new Intent(this, TargetActivity.class);
intent.putExtra("key1", "value1");
intent.putExtra("key2", 123);
startActivity(intent);
// Receiving data
Intent receivedIntent = getIntent();
String value1 = receivedIntent.getStringExtra("key1");
int value2 = receivedIntent.getIntExtra("key2", 0);
Intent Filters​
- Purpose: Declare what kinds of intents a component can handle
- Location: Defined in AndroidManifest.xml
- Components: Action, data, category
Example:
<activity android:name=".MyActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http" />
</intent-filter>
</activity>
b) Toolbar in Android and its Usage​
Definition​
Toolbar is a flexible widget that serves as a replacement for the traditional ActionBar, providing a more customizable and material design-compliant app bar.
Key Features​
- Material Design: Follows Material Design guidelines
- Customization: Highly customizable appearance and behavior
- Flexibility: Can be placed anywhere in the layout
- Backwards Compatibility: Works with older Android versions through AppCompat
Setting up Toolbar​
1. Add to Layout:
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
2. Setup in Activity:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Optional: Enable up button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("My App");
}
}
Toolbar Components​
1. Navigation Icon
toolbar.setNavigationIcon(R.drawable.ic_menu);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Handle navigation click
}
});
2. Title and Subtitle
toolbar.setTitle("Main Title");
toolbar.setSubtitle("Subtitle");
3. Menu Items
- Create menu resource file (
res/menu/toolbar_menu.xml
):
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_search"
android:icon="@drawable/ic_search"
android:title="Search"
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_settings"
android:title="Settings"
app:showAsAction="never" />
</menu>
- Handle menu events:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.toolbar_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:
// Handle search
return true;
case R.id.action_settings:
// Handle settings
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Customization Options​
- Colors: Background, text, icon colors
- Typography: Font, text size, style
- Layout: Height, padding, margins
- Elevation: Shadow depth
- Animation: Scroll behavior, transitions
Best Practices​
- Consistent Design: Follow Material Design guidelines
- Important Actions: Place primary actions as icons
- Overflow Menu: Use for secondary actions
- Navigation: Provide clear navigation cues
- Responsive: Adapt to different screen sizes